﻿@inject IDataCacheService _dataCacheService

@inject IPopupService PopupService

<div class="@Class">
    <div class="pb-2" style="display: flex; justify-content: center; text-align: center;">
        目前只支持B站链接，格式如下<br />
        https://www.bilibili.com/video/BV1tF411N7XD
    </div>
    <MRow Class="mb-4">
        <MCol Cols="12" Md="3">
            <MTextField Label="类型（可不填，将自动获取）" @bind-Value="@InputModel.Type" TValue="string"></MTextField>

        </MCol>
        <MCol Cols="12" Md="9">
            <MTextField Label="标题（可不填，将自动获取）" @bind-Value="@InputModel.Title" TValue="string"></MTextField>

        </MCol>
        <MCol Cols="12">
            <MTextField Label="链接" @bind-Value="@InputModel.Url" TValue="string"></MTextField>

        </MCol>
        <MCol Cols="12">
            <MCheckbox @bind-Value="InputModel.IsCreatedByCurrentUser" Label="本人创作" />

        </MCol>
        <MCol Cols="12">
            <CnGalWebSite.Components.Buttons.MasaButton Rounded Text="添加" OnClick="@(async ()=> {await OnAdd(InputModel);InputModel = new RepostVideoModel(); })" Icon="mdi-plus" Class="me-4" />
             <CnGalWebSite.Components.Buttons.MasaButton Rounded Text="批量导入" OnClick="OnImport" Icon="mdi-database-import" />
         </MCol>
     </MRow>
     <MSimpleTable Class="mb-4">
         <thead>
             <tr>
                 <th class="text-left">
                     类型
                 </th>
                 <th class="text-left">
                     本人创作
                 </th>
                 <th class="text-left">
                     标题
                 </th>
                 <th class="text-left">
                     链接
                 </th>
                 <th class="text-left">
                     操作
                 </th>
             </tr>
         </thead>
         <tbody>
             @foreach (var item in Model)
            {
                <tr @key="item.Url">
                    <td>@item.Type</td>
                    <td>
                        @if (item.IsCreatedByCurrentUser)
                        {
                            <MIcon Color="success">mdi-check</MIcon>
                        }
                        else
                        {
                            <MIcon Color="error">mdi-close</MIcon>
                        }
                    </td>
                    <td>@item.Title</td>
                    <td>@item.Url</td>
                    <td>
                        <CnGalWebSite.Components.Buttons.MasaButton Text="删除" TextStyle OnClick="@(()=>OnDelete(item))" Icon="mdi-trash-can" Color="error" />
                     </td>
                 </tr>
            }
        </tbody>
    </MSimpleTable>

    <CnGalWebSite.Components.Buttons.MasaButton Text="下一步" Rounded Icon="mdi-arrow-down-bold-outline" OnClick="OnClickFinshInput" Class="mb-2" />
 </div>
 <ImportCard @ref="importCard" OnImported="OnImported" />

 @code {
    [Parameter]
    public List<RepostVideoModel> Model { get; set; }
    [Parameter]
    public string Class { get; set; }
    [Parameter]

    public EventCallback OnFinshInput { get; set; }
    RepostVideoModel InputModel = new RepostVideoModel();

    ImportCard importCard;

    protected override async Task OnInitializedAsync()
    {

    }
    public async Task OnImported(List<string> items)
    {
        foreach (var item in items)
        {
            var model = new RepostVideoModel
                {
                    Url = item,
                    IsCreatedByCurrentUser = InputModel.IsCreatedByCurrentUser
                };
            await OnAdd(model);
        }
    }
    public async Task OnImport()
    {
        await importCard.Refresh(true);
    }

    public async Task OnAdd(RepostVideoModel model)
    {
        model.Url = model.Url.Split('?').FirstOrDefault();

        if (string.IsNullOrWhiteSpace(model.Url))
        {
            await PopupService.ToastAsync("先填写视频链接哦", AlertTypes.Error);
            return;
        }
        if (!model.Url.Contains("www.bilibili.com"))
        {
            await PopupService.ToastAsync("链接格式不正确", AlertTypes.Error);
            return;
        }
        if (Model.Any(s => s.Url == model.Url))
        {
            await PopupService.ToastAsync("视频链接添加一次就够了哦", AlertTypes.Error);
            return;
        }



        Model.Add(model);
    }

    public void OnDelete(RepostVideoModel model)
    {
        Model.Remove(model);

    }

    public async Task OnClickFinshInput()
    {
        if (Model.Count == 0)
        {
            await PopupService.ToastAsync("你还没有添加需要转载的视频链接哦", AlertTypes.Error);
            return;
        }
        await OnFinshInput.InvokeAsync();
    }
}
